// pages/shopping/activedetail/index.js
const Sharing = require('../../../utils/extend/sharing.js');
const App = getApp();
const Dialog = require('../../../components/dialog/dialog');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        maskshow: false,
        checked: false
    },
    handleurl(){
        wx.navigateTo({
          url: '/pages/sharing/order/index',
        })
    },
    /**
     * 跳转商品详情页
     */
    onTargetGoods() {
        if (!this.data.goods_id) {
            App.showError("请选择商品")
        } else if (!this.data.checked) {
            App.showError("请勾选协议")
        } else {
            if(this.data.cateInfo.has_sharing_num==0){
                App.showError("您的免费参团资格已用完，请您先去购买",()=>{
                    wx.navigateTo({
                      url: './buy',
                    })
                })
            }else{
                if(!this.data.cateInfo.active.active_id){
                    App.showError("活动暂未开始")
                    return;
                   }
                wx.navigateTo({
                    url: '/pages/sharing/goods/index?goods_id=' + this.data.goods_id+"&&active_id="+this.data.cateInfo.active.active_id
                });
            }

           
        }

    },
    //   勾选协议
    btnchecked() {
        this.setData({
            checked: !this.data.checked
        })
    },
    radioChange(e) {
        console.log(e)
        this.setData({
            goods_id: e.detail.value
        })
    },
    // 获取专区信息
    fetchdetail(id) {
        App._get('sharing.goods/lists', {
            category_id:id
        }, result => {
            let cateInfo = result.data.cateInfo
            let time = new Date(cateInfo.first_create_group_time).getTime()
            let endtime = new Date(cateInfo.active.end_time).getTime()
            if (this.data.nowtime < time) {
                this.setData({
                    countname: "活动未开始"
                })
            } else {
                if (cateInfo.active.has_active == 0) {
                    this.setData({
                        countname: "暂无活动"
                    })
                } else if (cateInfo.active.has_active == 1) {

                    if (this.data.nowtime > endtime) {
                        this.setData({
                            countname: "活动已结束"
                        })
                    } else {
                        this.setData({
                            countname: "距结束"
                        })
                    }

                }

            }
            // cateInfo.active.end_time 
            this.setData(result.data)
            console.log(result.data)
            console.log(new Date(result.data.cateInfo.active.end_time).getTime())
            console.log(new Date().getTime())
        });
    },
    // 打开玩法规则
    handleOpen() {
        this.setData({
            maskshow: true
        })
    },
    // 关闭玩法规则
    // handleClose(){
    //     this.setData({
    //         maskshow:false
    //     })
    // },
    /**
     * 显示拼团协议
     */
    toggleApplyLicense() {
        Dialog({
            title: '申请协议',
            message: this.data.setting.basic.agreement_detail,
            selector: '#zan-base-dialog',
            isScroll: true, // 滚动
            buttons: [{
                text: '我已阅读',
                color: 'red',
                type: 'cash'
            }]
        }).then(() => {
            // console.log('=== dialog resolve ===', 'type: confirm');
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.fetchdetail(options.category_id)
        this.setData({
            nowtime: new Date().getTime()
        })
        // 获取拼团设置
        this.getSetting();
    },
    /**
     * 获取拼团设置
     */
    getSetting() {
        let _this = this;
        Sharing.getSetting(setting => {
            _this.setData({
                setting
            });
        });
    },
    /**
     * 分享当前页面
     */
    onShareAppMessage() {
        const _this = this;
        // 构建页面参数
        const params = App.getShareUrlParams({
            'goods_id': _this.data.goods_id
        });
        return {
            title: _this.data.detail.goods_name,
            path: "/pages/sharing/goods/index?" + params
        };
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if(this.data.cateInfo.category_id){
            this.fetchdetail(this.data.cateInfo.category_id)
        }
       
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})